package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.util.AlbumManageUtil;

public class FriendInfoProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        BusinessProtocolMessages.FriendInfoProtocol response = (BusinessProtocolMessages.FriendInfoProtocol) msg;
        if (response.hasRegisteredFriend()) {
            BusinessProtocolEntities.RegisteredFriend registeredFriend = response.getRegisteredFriend();
            FriendTable friendTable = getFriendTable(IMSUtils.userTypeAnalysis(registeredFriend.getFriendUserId()), registeredFriend.getFriendId(), registeredFriend.getFriendUserId(), registeredFriend.getFriendNickname(), registeredFriend.getFriendAvatar(), registeredFriend.getFriendAlias(), registeredFriend.getUserType(), registeredFriend.getOnlineStatus(), registeredFriend.hasSettings(), registeredFriend.getSettings());

            if (isOnScreen) {
                AlbumManageUtil.getInstance().downLoadFileFriend(registeredFriend.getFriendAvatar(), AlbumTableBean.TYPE_VIDEO, friendTable.getFriendUserId(), new AlbumManageUtil.OnFileResponse() {
                    @Override
                    public void onResult(String path, String fileId) {
                        friendTable.setFriendAvatarId(fileId);
                        update(friendTable);
                    }
                });
            } else {
                update(friendTable);
            }
        }
        if (response.hasTouristsFriend()) {
            BusinessProtocolEntities.TouristsFriend touristsFriend = response.getTouristsFriend();
            FriendTable friendTable = getFriendTable(IMSUtils.userTypeAnalysis(touristsFriend.getFriendUserId()), touristsFriend.getFriendId(), touristsFriend.getFriendUserId(), touristsFriend.getFriendNickname(), touristsFriend.getFriendAvatar(), touristsFriend.getFriendAlias(), touristsFriend.getUserType(), touristsFriend.getOnlineStatus(), touristsFriend.hasSettings(), touristsFriend.getSettings());
            update(friendTable);
            if (isOnScreen) {
                AlbumManageUtil.getInstance().downLoadFileFriend(touristsFriend.getFriendAvatar(), AlbumTableBean.TYPE_VIDEO, friendTable.getFriendUserId(), new AlbumManageUtil.OnFileResponse() {
                    @Override
                    public void onResult(String path, String fileId) {
                        friendTable.setFriendAvatarId(fileId);
                        update(friendTable);
                    }
                });
            } else {
                update(friendTable);
            }
        }

    }

    private FriendTable getFriendTable(String registeredUser, String friendId, String friendUserId, String friendNickname, String friendAvatar, String friendAlias, String userType, String onlineStatus, boolean b, BusinessProtocolEntities.Settings settings) {
        FriendTable friendTable =FriendTable.getRowByFriendUserIdNotNull(friendUserId);
        friendTable.setUserType(registeredUser);
        friendTable.setFriendId(friendId);
        friendTable.setFriendUserId(friendUserId);
        friendTable.setFriendNickname(friendNickname);
        friendTable.setFriendAvatar(friendAvatar);
        friendTable.setFriendAlias(friendAlias);
        friendTable.setUserType(IMSUtils.userTypeAnalysis(friendUserId));
        friendTable.setOnlineStatus(onlineStatus);
        if (b) {
            BusinessProtocolEntities.Settings set = settings;
            friendTable.setOfflineMessage(set.getOfflineMessage());
            friendTable.setSnapchatStatus(set.getSnapchatStatus());
            friendTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            friendTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return friendTable;
    }

    @Override
    public void update(Object... table) {
        super.insert(table);
        try {
            FriendTable friendTable = (FriendTable) table[0];
            FriendTable.insert(friendTable);

//            ChatContentTable chatTableBean = new ChatContentTable();
//            chatTableBean.setChatType(Canstants.SINGLE_CHAT);
//            chatTableBean.setTargetId(friendTable.getFriendUserId());
//            chatTableBean.setTargetName(friendTable.getFriendNickname());
//            chatTableBean.setTargetType(friendTable.getUserType());
//            chatTableBean.setTargetAvatarUrl(friendTable.getFriendAvatar());
//            ChatContentTable.insert(chatTableBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
